﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;

namespace YJKOpeningCommon.Opening2D
{
	public class CreateOpening2D
	{
		public CreateOpening2D(Document doc)
		{
			this.m_doc = doc;
		}

		public GraphicsStyle GetOpening2DLineStyle()
		{
			string text = "洞口二维符族号轮廓线";
			Category category = this.m_doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return null;
			}
			if (!category.SubCategories.Contains(text))
			{
				Transaction transaction = new Transaction(this.m_doc);
				try
				{
					transaction.Start("new line style");
					Category category2 = this.m_doc.Settings.Categories.NewSubcategory(category, text);
					category2.LineColor = new Color(0, 0, 0);
					category2.SetLineWeight(10, (Autodesk.Revit.DB.GraphicsStyleType)1);
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
					{
						transaction.RollBack();
					}
					return null;
				}
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc).OfClass(typeof(GraphicsStyle));
			foreach (Element element in filteredElementCollector)
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle.Name.CompareTo(text) == 0)
				{
					return graphicsStyle;
				}
			}
			return null;
		}

		public LinePatternElement GetOpening2DLineStylePattern()
		{
			string text = "洞口二维符族号轮廓线线型图案";
			LinePatternElement linePatternElement = LinePatternElement.GetLinePatternElementByName(this.m_doc, text);
			if (linePatternElement == null)
			{
				List<LinePatternSegment> list = new List<LinePatternSegment>();
                list.Add(new LinePatternSegment((LinePatternSegmentType)0, AssistFunc.mmToFeet(2.0)));
                list.Add(new LinePatternSegment((LinePatternSegmentType)1, AssistFunc.mmToFeet(1.0)));
				LinePattern linePattern = new LinePattern(text);
				linePattern.SetSegments(list);
				Transaction transaction = new Transaction(this.m_doc);
				try
				{
					transaction.Start("Create a linepattern element");
					linePatternElement = LinePatternElement.Create(this.m_doc, linePattern);
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
				}
			}
			return linePatternElement;
		}

		public FilledRegionType GetOpening2DFilledRegionType()
		{
			string text = "HYOpening2DFilledRegionType";
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc).OfClass(typeof(FilledRegionType));
			FilledRegionType filledRegionType = null;
			FilledRegionType filledRegionType2 = null;
			foreach (Element element in filteredElementCollector)
			{
				FilledRegionType filledRegionType3 = element as FilledRegionType;
				if (filledRegionType2 == null)
				{
					filledRegionType = filledRegionType3;
				}
				if (filledRegionType3.Name.CompareTo(text) == 0)
				{
					filledRegionType2 = filledRegionType3;
					break;
				}
			}
			if (filledRegionType2 != null)
			{
				return filledRegionType2;
			}
			if (filledRegionType == null)
			{
				return null;
			}
			FillPatternElement solidPattern = this.GetSolidPattern("实体填充");
			if (solidPattern == null)
			{
				return null;
			}
			Transaction transaction = new Transaction(this.m_doc, "CopyFilledRegionType");
			try
			{
				transaction.Start();
				filledRegionType2 = (filledRegionType.Duplicate(text) as FilledRegionType);
				filledRegionType2.FillPatternId = solidPattern.Id;
				filledRegionType2.Color = new Color(byte.MaxValue, byte.MaxValue, byte.MaxValue);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			return filledRegionType2;
		}

		private FillPatternElement GetSolidPattern(string patternName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			return filteredElementCollector.OfClass(typeof(FillPatternElement)).Cast<FillPatternElement>().FirstOrDefault<FillPatternElement>();
		}

		private Document m_doc;
	}
}
